package flc.ast.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupPosition;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.bean.MyScaleBean;
import flc.ast.databinding.ActivitySetPwdBinding;
import flc.ast.dialog.QuestionDialog;
import flc.ast.dialog.ScaleDialog;
import flc.ast.utils.Content;
import flc.ast.view.PasscodeView;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;

public class SetPwdActivity extends BaseAc<ActivitySetPwdBinding> {

    private BasePopupView questionPopup;
    private QuestionDialog questionDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_set_pwd;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivSetPwdBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.llQuestion.setOnClickListener(this);
        mDataBinding.tvSetAnswerNext.setOnClickListener(this);
        mDataBinding.passcodeView.setListener(new PasscodeView.PasscodeViewListener() {
            @Override
            public void onFail() {
                mDataBinding.passcodeView.getWrongStatusColor();
            }

            @Override
            public void onSuccess(String number) {
                SPUtil.putString(mContext, Content.myPassword, number);
                mDataBinding.llSetSecure.setVisibility(View.VISIBLE);
                mDataBinding.llSetPwd.setVisibility(View.GONE);
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.llQuestion) {
            showQuestionDialog();
        } else if (id == R.id.tvSetAnswerNext) {
            String answer = mDataBinding.etSetAnswerText.getText().toString().trim();
            String problem = mDataBinding.tvSelAnswerProblem.getText().toString();
            if (answer.isEmpty()) {
                ToastUtils.showShort(getString(R.string.please_input_answer));
                return;
            }
            SPUtil.putString(mContext, Content.myAnswer, answer);
            SPUtil.putString(mContext, Content.myProblem, problem);
            SPUtil.putBoolean(mContext, Content.isHavePassword, true);
            ToastUtils.showShort(R.string.password_set_suc);
            startActivity(HomeActivity.class);
        }
    }

    @Override
    protected void initData() {

    }

    private void showQuestionDialog() {
        if (questionDialog == null) questionDialog = new QuestionDialog(mContext);
        questionPopup = new XPopup.Builder(mContext).popupPosition(PopupPosition.Right) //手动指定弹窗的位置
                .offsetX(-120)//x轴偏移
                .isDestroyOnDismiss(false) //对于只使用一次的弹窗，推荐设置这个
                .atView(mDataBinding.tvSelAnswerProblem).hasShadowBg(false) // 去掉半透明背景
                .asCustom(questionDialog);
        questionDialog.setListener(new QuestionDialog.Ilistener() {

            @Override
            public void click(String question) {
                mDataBinding.tvSelAnswerProblem.setText(question);
            }
        });
        questionPopup.show();
    }

}